<%@ page pageEncoding="UTF-8"%>
<%@ include file="import.jsp"%>
<%!
	// 创建时间轴
	public Message createTimeLine(Map<String, String> parameters, Token token) {
		Message msg = new Message();
		Timeline timeline = Timeline.fromMap(parameters, Timeline.class);
		switch (timeline.getKind()) {
		case 1:
			timeline.setName("手术类型");
			break;
		case 2:
			timeline.setName("复诊随访");
			break;
		case 3:
			timeline.setName("问卷随访");
			break;
		case 4:
			timeline.setName("其它");
			break;
		case 5:
			timeline.setName("基本资料");
			break;
		case 6:
			timeline.setName("定期监测");
			break;
		default:
			break;
		}
		timeline.setModUid(token.getUid());
		timeline.setDoctorUid(token.getUid());
		Integer id = createItem(timeline);
		if (id == null) {
			setErrcode(msg, FAILURE);
		} else {
			msg.put("tid", id + "");
		}
		return msg;
	}

	// 修改时间轴
	public Message modifyTimeLine(Map<String, String> parameters, Token token) {
		Message msg = new Message();
		Timeline timeline = Timeline.fromMap(parameters, Timeline.class);
		timeline.setModUid(token.getUid());
		if (!modifyItem(timeline)) {
			setErrcode(msg, NO_RESULT);
		}
		return msg;
	}

	// 删除时间轴
	public Message destroyTimeLine(Map<String, String> parameters, Token token) {
		Message msg = new Message();
		Timeline timeline = Timeline.fromMap(parameters, Timeline.class);
		timeline.setModUid(token.getUid());
		timeline.setStatus(STATUS_DISABLE);
		if (!modifyItem(timeline)) {
			setErrcode(msg, NO_RESULT);
		}
		return msg;
	}

	// 查询时间轴
	public Message queryTimeLine(Map<String, String> parameters) {
		Message msg = new Message();
		Timeline timeline = Timeline.fromMap(parameters, Timeline.class);
		List list = QBuilder.db("basedb").queryHql("select new map(t.id as id, t.date as date, (case when t.treatProgress is NULL then '' else t.treatProgress end) as treat_progress, (case when t.treatEstimate is NULL then '' else t.treatEstimate end) as treat_estimate, (case when t.type is NULL then '' else t.type end) as type, (case when t.kind is NULL then 0 else t.kind end) as category, (case when t.oid is NULL then '' else t.oid end) as oid, (case when t.name is NULL then '' else t.name end) as name, (select v.isconfirmed from Visit as v where t.kind in (" + TIME_LINE_FOLLOW + "," + TIME_LINE_QUESTION + ") and v.id = t.oid) as visit_state, (select o.state from Order as o where t.kind = " + TIME_LINE_OPERATE + " and o.id = t.oid) as order_state) from Timeline as t where t.hospital = " + timeline.getHospital() + " and t.status = " + STATUS_ENABLE + " and t.patientUid = " + timeline.getPatientUid() + " order by t.date desc, t.cTime desc");
		if (list.size() == 0) {
			setErrcode(msg, NO_RESULT);
			msg.put("time_lines", list);
			return msg;
		}
		Map<String, String> years = new HashMap<String, String>();
		List result = new ArrayList();
		for (int i = 0; i < list.size(); i++) {
			Map map = (Map)list.get(i);
			try{
				String date = DateUtil.getYear(DateUtil.parseDate(map.get("date") + "")) + "";
				years.put(date, date);
			} catch (Exception e) {
				continue;
			}
		}
		Set<String> keys = years.keySet();
		List<String> tempList = new ArrayList<String>();
		tempList.addAll(keys);
		Collections.sort(tempList, new Comparator<String>() {

			@Override
			public int compare(String o1, String o2) {
				return - o1.compareTo(o2);
			}
		});
		for (String key : tempList) {
			List resultList = new ArrayList();
			Iterator it = list.iterator();
			while (it.hasNext()) {
				Map map = (Map)it.next();
				int category = Tools.getInt(map.get("category") + "", -9999);
				if (category == TIME_LINE_OPERATE) {
					Integer state = (Integer)map.get("order_state");
					if (state == null)
						map.put("order_state", STATE_OVER);
				}
				try{
					String date = DateUtil.getYear(DateUtil.parseDate(map.get("date") + "")) + "";
					if (key.equals(date)) {
						resultList.add(map);
						it.remove();
					}
				} catch (Exception e) {
					continue;
				}
			}
			if (resultList.size() > 0) {
				Map resultMap = new HashMap();
				resultMap.put("year", key);
				resultMap.put("time_line", resultList);
				result.add(resultMap);
			}
		}
		msg.put("time_lines", result);
		return msg;
	}

	// 查询手术类型
	public Message queryOperateType(Map<String, String> parameters) {
		Message msg = new Message();
		Service service = Service.fromMap(parameters, Service.class);
		List<Service> sersvices = (List<Service>)queryItems(service);
		if (sersvices.size() == 0) {
			setErrcode(msg, NO_RESULT);
			return msg;
		}
		msg.put("types", sersvices);
		return msg;
	}
%>
